﻿using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;


namespace DocuChart.PieChart
{
    [AspNetHostingPermission(SecurityAction.Demand,
   Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]


    [ToolboxItemAttribute(false)]
    public class PieChart : WebPart
    {

        [Personalizable(), WebBrowsable(false)]
        public string _SelectedList { get; set; }

        [Personalizable(), WebBrowsable(false)]
        public string _ChartType { get; set; }

        [Personalizable(), WebBrowsable(false)]
        public bool _3d { get; set; }

        [Personalizable(), WebBrowsable(false)]
        public bool _Highlight { get; set; }

        [Personalizable(), WebBrowsable(false)]
        public bool _Totals { get; set; }

        [Personalizable(), WebBrowsable(false)]
        public bool _Legend { get; set; }

        [Personalizable(), WebBrowsable(false)]
        public string _Palette { get; set; }


        

        protected override void CreateChildControls()
        {

            if (_SelectedList != null)
            {

                string siteURL = SPContext.Current.Site.Url;
                SPSite curSite = new SPSite(siteURL);
                SPWeb curWeb = curSite.OpenWeb();
                SPUser curUser = curWeb.CurrentUser;

                SPQuery curQry = new SPQuery();
                curQry.Query = "<Where></Where>";
                curQry.RowLimit = 100;
                SPList myList = curWeb.Lists[_SelectedList];
                             
                SPListItemCollection curItems = myList.GetItems(curQry);


                Label label1 = new Label();
                Hashtable hash = new Hashtable();


                foreach (SPListItem curItem in curItems)
                {
                    string CreatedBy = curItem["Created By"].ToString();
                    int Index = CreatedBy.IndexOf("#");
                    CreatedBy = CreatedBy.Substring(Index + 1);

                    if (hash.ContainsKey(CreatedBy))
                    {
                        int i;
                        i = Convert.ToInt32(hash[CreatedBy]);
                        i += 1;
                        hash[CreatedBy] = i;

                    }
                    else
                    {
                        hash.Add(CreatedBy, 1);

                    }



                }

                curWeb.Dispose();
                curSite.Dispose();



                    ArrayList xValues = new ArrayList();
                    ArrayList yValues = new ArrayList();
                    int j = 0;
                    int k = 0;
                    foreach (DictionaryEntry entry in hash)
                    {

                        string MyKey = Convert.ToString(entry.Key);
                        if (MyKey == curUser.Name)
                        {
                            k = j;
                        }
                        
                        if(_Totals){
                        string _title = Convert.ToString(entry.Key) + " " + Convert.ToString(entry.Value);
                            xValues.Add(_title);
                        yValues.Add(entry.Value);
                        j++;
                        } else {
                        xValues.Add(entry.Key);
                        yValues.Add(entry.Value);
                        j++;

                        }

                    }


                    Chart MyChart = new Chart();                 
                    MyChart.ChartAreas.Add(new ChartArea());
                    MyChart.Legends.Add(new Legend());
                    MyChart.Series.Add(new Series());
                    MyChart.Legends[0].Enabled = false;
                
                    switch (_ChartType)
                    {
                        
                        case "Bar":
                            MyChart.Series[0].ChartType = SeriesChartType.Bar;
                            MyChart.Legends[0].Enabled = false;

                            
                            break;
                        case "Pie":
                            MyChart.Series[0].ChartType = SeriesChartType.Pie;
                            MyChart.Series[0].CustomProperties = "PieLabelStyle=Disabled";
                            break;
                    }



                  if (_Legend && _ChartType == "Pie")
                    {

                      
                      MyChart.Legends[0].Enabled = true;
                    }

                    if (_3d)
                    {
                        MyChart.ChartAreas[0].Area3DStyle.Enable3D = true;
                    }
                    
                
                MyChart.Series[0].Points.DataBindXY(xValues.ToArray(), yValues.ToArray());

                    if (xValues.Count != 0 && yValues.Count != 0 && MyChart.Series[0].ChartType.Equals(SeriesChartType.Pie)  && _Highlight.Equals(true))
                    {
                      MyChart.Series[0].Points[k].CustomProperties = "Exploded=true";
                        
                    }

                    if (xValues.Count != 0 && yValues.Count != 0 && MyChart.Series[0].ChartType.Equals(SeriesChartType.Bar) && _Highlight.Equals(true))
                    {
                        MyChart.Series[0].Points[k].Color = Color.Red;
                        
                    }

                    switch (_Palette)
                    {
                        case "Bright":
                            MyChart.Series[0].Palette = ChartColorPalette.Bright;
                            break;
                        case "Grayscale":
                            MyChart.Series[0].Palette = ChartColorPalette.Grayscale;
                            break;
                        case "Excel":
                            MyChart.Series[0].Palette = ChartColorPalette.Bright;
                            break;
                        case "Light":
                            MyChart.Series[0].Palette = ChartColorPalette.Excel;
                            break;
                        case "Pastel":
                            MyChart.Series[0].Palette = ChartColorPalette.Pastel;
                            break;
                        case "EarthTones":
                            MyChart.Series[0].Palette = ChartColorPalette.EarthTones;
                            break;
                        case "SemiTransparent":
                            MyChart.Series[0].Palette = ChartColorPalette.SemiTransparent;
                            break;
                        case "Berry":
                            MyChart.Series[0].Palette = ChartColorPalette.Berry;
                            break;
                        case "Chocolate":
                            MyChart.Series[0].Palette = ChartColorPalette.Chocolate;
                            break;
                        case "Fire":
                            MyChart.Series[0].Palette = ChartColorPalette.Fire;
                            break;
                        case "SeaGreen":
                            MyChart.Series[0].Palette = ChartColorPalette.SeaGreen;
                            break;
                        case "BrightPastel":
                            MyChart.Series[0].Palette = ChartColorPalette.BrightPastel;
                            break;
                                                
                    }
                    MyChart.ImageStorageMode = ImageStorageMode.UseImageLocation;

                    MyChart.Width = 500;
                    MyChart.Height = 300;
                    MyChart.RenderType = RenderType.ImageTag;
                    string imagePath = "~/_layouts/ChartImages/";
                    MyChart.ImageLocation = imagePath + "ChartPic_#SEQ(200,30)";
                    Controls.Add(MyChart);
               
                }
        }
                

        public override EditorPartCollection CreateEditorParts()
        {
            ArrayList alControls = new ArrayList();
            alControls.Add(new DocuChart.PieChartEditor.PieChartWebpartEditor() { ID = this.ID + "_DocuChartEditor", Title = "Chart", ChromeState = PartChromeState.Normal });       
            EditorPartCollection epc = new EditorPartCollection(alControls);
            return epc;                   
        }

            }

 }

        

    
    

    

